package com.sx.gongzhong.calender.weiget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

import com.sx.gongzhong.calender.bean.AttrsBean;
import com.sx.gongzhong.calender.listener.OnPagerChangeListener;
import com.sx.gongzhong.calender.utils.CalendarUtil;
import com.sx.gongzhong.calender.utils.SolarUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class CalendarView extends ViewPager {
    //记录当前PagerAdapter的position
    private int currentPosition;

    private OnPagerChangeListener pagerChangeListener;

    private int[] initDate;//日历初始显示的年月
    private int[] startDate;//日历的开始年、月
    private int[] endDate;//日历的结束年、月

    private int count;//ViewPager的页数
    private int[] lastClickDate = new int[2];//记录单选的ViewPager position以及选中的日期
    private int startYear = 2017;
    private int endYear = 2049;
    private CalendarPagerAdapter calendarPagerAdapter;

    private AttrsBean mAttrsBean;

    public CalendarView(Context context) {
        this(context, null);
    }

    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mAttrsBean = new AttrsBean();
//        startDate = new int[]{1900, 1};
        startDate = new int[]{startYear, 1};
        endDate = new int[]{endYear, 12};
        mAttrsBean.setStartDate(this.startDate);
        mAttrsBean.setEndDate(this.endDate);
    }
    public void init() {

        //每个月选中统计数量
        List<Integer> monthCount = new ArrayList<>();
        mAttrsBean.setMonthCount(monthCount);

        //根据设定的日期范围计算日历的页数
        count = (endDate[0] - startDate[0]) * 12 + endDate[1] - startDate[1] + 1;
        calendarPagerAdapter = new CalendarPagerAdapter(count);
        calendarPagerAdapter.setAttrsBean(mAttrsBean);
        setAdapter(calendarPagerAdapter);

        currentPosition = CalendarUtil.dateToPosition(initDate[0], initDate[1], startDate[0], startDate[1]);

        setCurrentItem(currentPosition, false);

        addOnPageChangeListener(new SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                if (pagerChangeListener != null) {
                    int[] date = CalendarUtil.positionToDate(position, startDate[0], startDate[1]);
                    pagerChangeListener.onPagerChanged(new int[]{date[0], date[1], lastClickDate[1]});
//                    pagerChangeListener.onMonthCount(mAttrsBean.getMonthCount().get(position));//获取每月选中统计
                }
            }
        });
    }

    public void refreshMonthView(List<String> dates) {
        int[] currentYandM = CalendarUtil.positionToDate(currentPosition,startYear,1);
        int[] datesYandM = CalendarUtil.strToArray(dates.get(0));
        if (currentYandM[0] == datesYandM[0] && currentYandM[1] == datesYandM[1]){
            MonthView monthView = calendarPagerAdapter.getViews().get(currentPosition);
            HashSet<Integer> set = new HashSet<>();
            for (String date : dates) {
                int[] d = CalendarUtil.strToArray(date);
                if (isIllegal(d)) {
                    set.add(d[2]);
                }
            }
            monthView.refresh(set);
        }
    }

    /**
     * 计算 ViewPager 高度
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int calendarHeight;
        if (getAdapter() != null) {
            MonthView view = (MonthView) getChildAt(0);
            if (view != null) {
                calendarHeight = view.getMeasuredHeight();
                setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(calendarHeight, MeasureSpec.EXACTLY));
            }
        }
    }
    /**
     * 检查初始化选中的日期，或者要跳转的日期是否合法
     *
     * @param destDate
     * @return
     */
    private boolean isIllegal(int[] destDate) {
        if (destDate[1] > 12 || destDate[1] < 1) {
            return false;
        }
        if (CalendarUtil.dateToMillis(destDate) < CalendarUtil.dateToMillis(startDate)) {
            return false;
        }
        if (CalendarUtil.dateToMillis(destDate) > CalendarUtil.dateToMillis(endDate)) {
            return false;
        }
        if (destDate[2] > SolarUtil.getMonthDays(destDate[0], destDate[1]) || destDate[2] < 1) {
            return false;
        }
        return true;
    }

    /**
     * 设置月份切换回调
     *
     * @param pagerChangeListener
     */
    public void setOnPagerChangeListener(OnPagerChangeListener pagerChangeListener) {
        this.pagerChangeListener = pagerChangeListener;
    }
    /**
     * 设置日历初始显示的年月
     *
     * @param date
     * @return
     */
    public CalendarView setInitDate(String date) {
        initDate = CalendarUtil.strToArray(date);
        return this;
    }

    /**
     * 设置日历的开始年月、结束年月
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public CalendarView setStartEndDate(String startDate, String endDate) {
        this.startDate = CalendarUtil.strToArray(startDate);
        if (startDate == null) {
            this.startDate = new int[]{1900, 1};
        }
        this.endDate = CalendarUtil.strToArray(endDate);
        if (endDate == null) {
            this.endDate = new int[]{2049, 12};
        }
        mAttrsBean.setStartDate(this.startDate);
        mAttrsBean.setEndDate(this.endDate);
        return this;
    }

    /**
     * 设置默认选中的日期集合
     *
     * @param dates
     * @return
     */
    public CalendarView setDefaultDate(List<String> dates) {
        List<int[]> defaultDates = new ArrayList<>();
        for (String date : dates) {
            int[] d = CalendarUtil.strToArray(date);
            if (isIllegal(d)) {
                defaultDates.add(d);
            }
        }
        mAttrsBean.setSelectDefaultDates(defaultDates);
        return this;
    }
}
